package com.bulemobi.activity.ac_user_centre;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.bulemobi.adapter.MyCarAdapter;
import com.bulemobi.bean.CarInfoBean;
import com.bulemobi.App;
import com.bulemobi.net.tsz.afinal.FinalHttp;
import com.bulemobi.net.tsz.afinal.annotation.view.ViewInject;
import com.bulemobi.net.tsz.afinal.http.AjaxCallBack;
import com.bulemobi.net.tsz.afinal.http.AjaxParams;
import com.bulemobi.net.tsz.afinal.mynet.InnerNetUtil;
import com.bulemobi.urlCentre.UrlCentre;
import com.bulemobi.util.GetResourceUtil;
import com.bulemobi.util.LogUtil;
import com.bulemobi.util.ToastUtil;
import com.bulemobi.view.BaseActivity;
import com.bulemobi.view.pulltorefreshlist.library.PullToRefreshListView;
import com.bulemobi.view.titleView.ActionBarView;
import com.jsyh.hcb.R;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 武 on 2014/9/17 0017.
 * 我的车辆
 * 违章查询-有车辆
 * item: ac_mycar_item 已完成
 */
public class Ac_MyCar extends BaseActivity implements View.OnClickListener {

    @ViewInject(id = R.id.actionBar)
    ActionBarView actionBarView;
    @ViewInject(id = R.id.lv_myCar)
    PullToRefreshListView lv_myCar;
    @ViewInject(id = R.id.tv_addCar)
    TextView tv_addCar;
    @ViewInject(id = R.id.lay_no_car)
    LinearLayout lay_no_car;
    @ViewInject(id = R.id.lay_have_car)
    LinearLayout lay_have_car;
    MyCarAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_user_centre_addcar);
        LogUtil.d("我的的车辆");
        //从上页面传入的标示,用来判断List是否跳转,
        Intent intent = getIntent();

        findActionBar();
        initView();
        AjaxParams params = new AjaxParams();
        requestHttp(UrlCentre.CarBindListQry, params, "qry");
    }


    private void requestHttp(String url, AjaxParams params, final String type) {
        //支持的分辨率
        //320x480 480*800 640*960 480*854 320*240 600*1024 640*960 640*1136
        int[] resolutionWidths = new int[]{240, 320, 480, 600, 640};
        String[] resolutionValues = new String[]{"320*240", "320x480", "480*800", "600*1024", "640*1136"};
        HashMap<String, String> resolutionsMap = new HashMap<String, String>();
        for (int ii = 0; ii < resolutionWidths.length; ++ii) {
            resolutionsMap.put(resolutionWidths[ii] + "", resolutionValues[ii]);
        }
        int minIndex = 0;
        int minDif = Integer.MIN_VALUE;
        for (int i = 0; i < resolutionWidths.length; ++i) {
            float difValue = resolutionWidths[i] - App.DISPLAY_WID;
            if (difValue < minDif) {//当前的差值小
                minIndex = i;
            }
        }
        LogUtil.d("index：" + minIndex);
        LogUtil.d("最合适的宽度：" + resolutionWidths[minIndex]);
        LogUtil.d("最合适的分辨率1：" + resolutionsMap.get(resolutionWidths[minIndex] + ""));
        params.put("ratio", resolutionsMap.get(resolutionWidths[minIndex]));
        FinalHttp http = new FinalHttp();
        LogUtil.d("我的车俩参数：" + params.toString());
        http.post(url, params, new AjaxCallBack<Object>() {
            @Override
            public void onSuccess(Object o) {
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    if (InnerNetUtil.getStatus(jsonObject)) {
                        Gson gson = new Gson();
                        List<CarInfoBean> tempList = gson.fromJson(jsonObject.optString("res"), new TypeToken<List<CarInfoBean>>() {
                        }.getType());
                        if (tempList == null || (tempList != null && tempList.size() <= 0)) {// 代表已经没有数据可以加载了
                            //显示没有车辆信息
                            lay_no_car.setVisibility(View.VISIBLE);
                            tv_addCar.setVisibility(View.VISIBLE);
                        } else {
                            //显示listview
                            actionBarView.button_right_icon1.setVisibility(View.VISIBLE);
                            actionBarView.button_right_icon2.setVisibility(View.VISIBLE);
                            lay_have_car.setVisibility(View.VISIBLE);
                            adapter.notifyDataSetChanged();
                        }
                    } else {
                        ToastUtil.showToast(GetResourceUtil.getString(R.string.state_error_msg));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                super.onSuccess(o);
            }
        });
    }

    private void initView() {
        tv_addCar.setOnClickListener(this);
        lv_myCar.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                TODO:这里要写判断,根据传入的值 判断是否跳转
            }
        });
        ArrayList<String> arrayList = new ArrayList<String>();
        for (int i = 0; i < 10; i++) {
            arrayList.add(i + "");
        }
//        adapter = new MyCarAdapter(this,arrayList,"");
        lv_myCar.setAdapter(adapter);

        //默认隐藏
        lay_no_car.setVisibility(View.GONE);
    }

    private void findActionBar() {
        actionBarView.title.setText(getResources().getText(R.string.ac_myCar));
        actionBarView.button_right_icon1.setVisibility(View.VISIBLE);
        actionBarView.button_right_icon2.setVisibility(View.VISIBLE);
        actionBarView.button_right_icon1.setBackgroundResource(R.drawable.ac_mycar_del_bt);
        actionBarView.button_right_icon2.setBackgroundResource(R.drawable.ac_mycar_add);
        actionBarView.button_right_icon2.setOnClickListener(this);
        actionBarView.button_right_icon1.setOnClickListener(this);

        actionBarView.button_right_icon1.setVisibility(View.INVISIBLE);
        actionBarView.button_right_icon2.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //删除车辆
            case R.id.actionbar_right_button_icon1:
                break;
            //添加车辆
            case R.id.actionbar_right_button_icon2:
                openActivity(AddCarActivity.class);
                break;
            case R.id.tv_addCar:
                openActivity(AddCarActivity.class);
                break;
        }
    }
}
